home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / tcclib.exe / FIELDS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-07  |  6.5 KB  |  296 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <mem.h>
  4. #include "tcclib.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. int  GComm( void );
  10. void AtSay( int x, int y, char *s );
  11. void Beep( void );
  12. void BlockErase( int x, int y, int xx, int yy );
  13. void NormalText( void );
  14. void ReverseText( void );
  15. char *RevJul( unsigned d );
  16. unsigned Julian( char *s );
  17. void     TcclibInitialize( void );
  18.  
  19. extern unsigned char A_REVERSE;
  20. extern unsigned char A_NORMAL;
  21.  
  22. int GetField( FieldStruc *Field )
  23. {
  24.     register int ch;
  25.     register int j=0;
  26.     register char *Ptr;
  27.     register int i;
  28.     char Temp[90];
  29.     int RetVal;
  30.     int y;
  31.     int x;
  32.     int Length;
  33.     char Format[20];
  34.     int CharTyped = 0;
  35.  
  36.     TcclibInitialize();
  37.  
  38.     y = Field->y;
  39.     x = Field->x;
  40.     Length = Field->Len;
  41.  
  42.     BlockErase ( x, y, x+Length-1, y );
  43.     ChangeBlock( x, y, x+Length-1, y, A_REVERSE );
  44.     ReverseText();
  45.  
  46.     if ( Field->Type ){
  47.         Ptr = Temp;
  48.         memset( Temp, 0, sizeof(Temp) );
  49.          /*
  50.          *    Non-zero Type indicates a non-textstring value.
  51.          */
  52.         switch( Field->Type ){
  53.             case F_INT:
  54.                 sprintf( Format, "%%%dd", Length );
  55.                 sprintf(Temp, Format, *(int *) Field->Address);
  56.                 break;
  57.             case F_INT0:
  58.                 sprintf( Format, "%%0%dd", Length );
  59.                 sprintf(Temp, Format, *(int *) Field->Address);
  60.                 break;
  61.             case F_CHAR:
  62.                 sprintf(Temp, "%c", *(Field->Address));
  63.                 break;
  64.             case F_LNG:
  65.                 sprintf( Format, "%%%dld", Length );
  66.                 sprintf(Temp, Format, *(long *) Field->Address);
  67.                 break;
  68.             case F_LNG0:
  69.                 sprintf( Format, "%%0%dld", Length );
  70.                 sprintf(Temp, Format, *(long *) Field->Address);
  71.                 break;
  72.             case F_DATE:
  73.                 sprintf(Temp, "%s", RevJul( *(unsigned *) Field->Address ) );
  74.                 break;
  75.             case F_DBL:
  76.                 sprintf( Format, "%%%d.%df", Length, Field->NumDecimals );
  77.                 sprintf(Temp, Format, *(double *) Field->Address);
  78.                 break;
  79.             case F_FLT:
  80.                 sprintf( Format, "%%%d.%df", Length, Field->NumDecimals );
  81.                 sprintf(Temp, Format, *(float *) Field->Address);
  82.                 break;
  83.             case F_BLN:
  84.                 sprintf(Temp, "%c", (*(Field->Address)) ? 'Y' : 'N' );
  85.                 break;
  86.         }
  87.     }
  88.     else
  89.          Ptr = Field->Address;
  90.  
  91.     AtSay( x, y, Ptr );
  92.  
  93.     j = 0;
  94.  
  95.     gotoxy( x, y );
  96.  
  97.     for( ;; ){
  98.  
  99.           switch( ch = GComm() ){
  100.  
  101.                case SHFT_TAB:
  102.                case CTL_RIGHT:
  103.                     RetVal = LEFT;
  104.                     goto end_GetField;
  105.  
  106.                case TAB:
  107.                case CTL_LEFT:
  108.                     RetVal = RIGHT;
  109.                     goto end_GetField;
  110.  
  111.                case CR:
  112.                case LF:
  113.                case DOWN:
  114.                case UP:
  115.                case PGDN:
  116.                case PGUP:
  117.                case HOME:
  118.                case END:
  119.                case F1:
  120.                case F2:
  121.                case F3:
  122.                case F4:
  123.                case F5:
  124.                case F6:
  125.                case F7:
  126.                case F8:
  127.                case F9:
  128.                case F10:
  129.                case ESC:
  130.                     RetVal = ch;
  131.                     goto end_GetField;
  132.  
  133.                case RIGHT :
  134.                     if ( !CharTyped ) {
  135.                         RetVal = RIGHT;
  136.                         goto end_GetField;
  137.                     }
  138.                     if ( j < Length ) {
  139.                        j++;
  140.                        if ( Ptr[j] == 0 )
  141.                            Ptr[j] = 32;
  142.                        gotoxy( wherex()+1, wherey() );
  143.                     }
  144.                     break;
  145.  
  146.                case LEFT :
  147.                     if ( !CharTyped ) {
  148.                         RetVal = LEFT;
  149.                         goto end_GetField;
  150.                     }
  151.                     if ( j > 0 ) {
  152.                        j--;
  153.                        gotoxy( wherex()-1, wherey() );
  154.                     }
  155.                     break;
  156.  
  157.                case INS:
  158.                     CharTyped++;
  159.                     for (i=Length; i>j; --i)
  160.                         Ptr[i] = Ptr[i-1];
  161.                     Ptr[j] = 32;
  162.                     Ptr[Length] = 0;
  163.                     AtSay( x, y, Ptr );
  164.                     gotoxy( x+j, y );
  165.                     break;
  166.  
  167.                case BS:
  168.                     if( j == 0 ) {
  169.                          Beep();
  170.                          break;
  171.                     }
  172.                     j--;
  173.  
  174.                case DEL:
  175.                     CharTyped++;
  176.                     for (i=j; i<Length; ++i) {
  177.                         Ptr[i] = Ptr[i+1];
  178.                     }
  179.                     AtSay( x, y, Ptr );
  180.                     gotoxy( x+j, y );
  181.                     break;
  182.  
  183.                default:
  184.                     if ( !CharTyped && ch != 32 && Field->Type != F_PTR && Field->Type != F_DATE) {
  185.                         memset( Ptr, 0, Length );
  186.                         BlockErase ( x, y, x+Length-1, y );
  187.                         ChangeBlock( x, y, x+Length-1, y, A_REVERSE );
  188.                     }
  189.                     CharTyped++;
  190.                     if ( ch > 31 && ch < 128 ) {
  191.                        if ( j < Length ) {
  192.                           Ptr[j++] = ch;
  193.                           putch( ch );
  194.                        }
  195.                     }
  196.                     break;
  197.           }
  198.     }
  199.  
  200. end_GetField:
  201.     switch ( Field->Type ){
  202.         case F_INT0:
  203.         case F_INT:
  204.             *(int *) Field->Address = atoi(Temp);
  205.             sprintf( Temp, Format, *(int *) Field->Address );
  206.             break;
  207.         case F_CHAR:
  208.             *(char *) Field->Address = Temp[0];
  209.             break;
  210.         case F_LNG0:
  211.         case F_LNG:
  212.             *(long *) Field->Address = atol(Temp);
  213.             sprintf( Temp, Format, *(long *) Field->Address );
  214.             break;
  215.         case F_DBL:
  216.             *(double *) Field->Address = (double) atof(Temp);
  217.             sprintf(Temp, Format, *(double *) Field->Address);
  218.             break;
  219.         case F_FLT:
  220.             *(float *) Field->Address = (float) atof(Temp);
  221.             sprintf(Temp, Format, *(float *) Field->Address);
  222.             break;
  223.         case F_DATE:
  224.             *(unsigned *) Field->Address = Julian( Temp );
  225.             sprintf(Temp, "%s", RevJul( *(int *) Field->Address ) );
  226.             break;
  227.         case F_BLN:
  228.             *(Field->Address) = ( toupper(Temp[0]) == 'Y' ) ? 1 : 0;
  229.             break;
  230.     };
  231.     Ptr[ Length ] = '\0';
  232.     NormalText();
  233.     BlockErase( x, y, x+Length-1, y );
  234.     AtSay( x, y, Ptr );
  235.     ChangeBlock( x, y, x+Length-1, y, A_NORMAL );
  236.     return( RetVal );
  237. }
  238.  
  239.  
  240. void PutField( FieldStruc *Field )
  241. {
  242.     register char *Ptr;
  243.     char Temp[90];
  244.     char Format[20];
  245.  
  246.     TcclibInitialize();
  247.  
  248.     if ( Field->Type ){
  249.         Ptr = Temp;
  250.         memset( Temp, 0, sizeof(Temp) );
  251.          /*
  252.          *    Non-zero Type indicates a non-textstring value.
  253.          */
  254.         switch( Field->Type ){
  255.             case F_INT:
  256.                 sprintf( Format, "%%%dd", Field->Len );
  257.                 sprintf(Temp, Format, *(int *) Field->Address);
  258.                 break;
  259.             case F_INT0:
  260.                 sprintf( Format, "%%0%dd", Field->Len );
  261.                 sprintf(Temp, Format, *(int *) Field->Address);
  262.                 break;
  263.             case F_CHAR:
  264.                 sprintf(Temp, "%c", *(Field->Address));
  265.                 break;
  266.             case F_LNG:
  267.                 sprintf( Format, "%%%dld", Field->Len );
  268.                 sprintf(Temp, Format, *(long *) Field->Address);
  269.                 break;
  270.             case F_LNG0:
  271.                 sprintf( Format, "%%0%dld", Field->Len );
  272.                 sprintf(Temp, Format, *(long *) Field->Address);
  273.                 break;
  274.             case F_DBL:
  275.                 sprintf( Format, "%%%d.%df", Field->Len, Field->NumDecimals );
  276.                 sprintf(Temp, Format, *(double *) Field->Address);
  277.                 break;
  278.             case F_FLT:
  279.                 sprintf( Format, "%%%d.%df", Field->Len, Field->NumDecimals );
  280.                 sprintf(Temp, Format, *(float *) Field->Address);
  281.                 break;
  282.             case F_DATE:
  283.                 sprintf(Temp, "%s", RevJul( *(int *) Field->Address ) );
  284.                 break;
  285.             case F_BLN:
  286.                 sprintf(Temp, "%c", (*(Field->Address)) ? 'Y' : 'N' );
  287.                 break;
  288.         }
  289.     }
  290.     else
  291.          Ptr = Field->Address;
  292.  
  293.     BlockErase( Field->x, Field->y, Field->x + Field->Len - 1, Field->y );
  294.     AtSay( Field->x, Field->y, Ptr );
  295. }
  296.